<?php

/**
 * This is the model class for table "tbl_menu".
 *
 * The followings are the available columns in table 'tbl_menu':
 * @property integer $Id
 * @property string $NoiDung
 * @property string $Link
 * @property integer $STT
 * @property integer $Menu_Id
 * @property integer $TrangThai
 * @property string $MoTa
 */
class menu extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_menu';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('NoiDung', 'required'),
			array('STT, Menu_Id, TrangThai', 'numerical', 'integerOnly'=>true),
			array('NoiDung', 'length', 'max'=>200),
			array('Link, MoTa', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, NoiDung, Link, STT, Menu_Id, TrangThai, MoTa', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => 'Mã Menu',
			'NoiDung' => 'Tên Menu',
			'Link' => 'Liên Kết',
			'STT' => 'Số Thứ Tự',
			'Menu_Id' => 'Menu Cha',
			'TrangThai' => 'Trạng Thái',
			'MoTa' => 'Mô Tả',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('NoiDung',$this->NoiDung,true);
		$criteria->compare('Link',$this->Link,true);
		$criteria->compare('STT',$this->STT);
		$criteria->compare('Menu_Id',$this->Menu_Id);
		$criteria->compare('TrangThai',$this->TrangThai);
		$criteria->compare('MoTa',$this->MoTa,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function get_menu($id)  {
		if (is_numeric($id)) {
			$menu = $this->find("Id = '".$id."'");
			return isset($menu) ? $menu : false;
		} else 
			return false;
	}
	
	public function isset_data() {
		$count_menu = $this->count("Id");
		return ($count_menu > 0) ? true : false; 
	}
	
	public function isset_data_menu_parent($id)  {
		if (is_numeric($id)) {
			$count_menu = $this->countBySql("SELECT * FROM tbl_menu WHERE Menu_Id = $id");
			return ($count_menu > 0) ? true : false;
		} else 
			return false;
	}
	
	public function get_list_data() {
		return CHtml::listData($this->findAll("Menu_Id is NULL"), "Id", "NoiDung");
	}
	
	public function LayMenuTheoMenu_Id($id = 0) {
		if ($id == 0) {
			$sql = "SELECT * FROM tbl_menu WHERE Menu_Id IS NULL AND TrangThai = 1 ORDER BY STT ASC";
			$data = $this->findAllBySql($sql);
			return isset($data) ? $data : false;
		} else {
			if (is_numeric($id)) {
				$sql = "SELECT * FROM tbl_menu WHERE Menu_Id = '".$id."' AND TrangThai = 1 ORDER BY STT ASC";
				$data = $this->findAllBySql($sql);
				return isset($data) ? $data : false;	
			} else 
				return false;
		}
	}
	
	public function get_menu_name($id){
		$sql="SELECT * FROM tbl_menu WHERE Id='".$id."'";
		$menu = $this->findAllBySql($sql);
		if(count($menu)>0)
			return $menu[0]->NoiDung;
		else 
			return false;
	}
	public function get_menu_()
	{
		return CHtml::listData(menu::model()->findAll("Menu_Id is Null"), "Id", "NoiDung");
		//return CHtml::listData(NewsCategories::model()->findAll("news_cate_id"), "news_cate_id", "news_cate_name");
	}
}